package com.lcm.basic.p7.set;

import com.lcm.basic.p7.set.bean.Person;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Coder-lcm
 * @date 2021/4/3
 *
 *      HashSet是无序的，底层是hashMap实现
 */
public class SetDemoForHashSet {
    public static void main(String[] args) {
        //如果不指定泛型的具体类型，则默认是Object类型的Set
        Set books = new HashSet();
        books.add("sadada");
        books.add(1313);
        books.add(Boolean.TRUE);
        books.add(null); //集合元素可以为null，且只能有一个null
        //因为set是唯一性集合，所以重复元素会插入失败
        System.out.println(books.add(1313)); //false
        System.out.println(books.add(null)); //false
        System.out.println(books.size());

        Person p1 = new Person(12,"lcm01");
        Person p2 = new Person(13,"lcm02");
        Set<Person> persons = new HashSet();
        persons.add(p1);
        persons.add(p2);
        System.out.println(persons .size());
        System.out.println("修改前p1：" + p1);
        persons.forEach(each -> {
            System.out.println(each);
        });
        p1.setId(13);
        p1.setName("lcm02");
        System.out.println(persons .size());

        System.out.println("修改后p1：" + p1);
        persons.forEach(each -> {
            System.out.println(each);
        });

    }
}
